<template>
  <div class="avue-tags"
       v-if="setting.tag"
       @click="contextmenuFlag=false">
    <!-- tag盒子 -->
    <div v-if="contextmenuFlag" class="avue-tags__contentmenu"
         :style="{left:contentmenuX+'px',top:contentmenuY+'px'}">
      <div class="item"
           @click="closeTag">{{ $t('tagsView.close') }}
      </div>
      <div class="item"
           @click="closeOthersTags">{{ $t('tagsView.closeOthers') }}
      </div>
      <div class="item"
           @click="closeAllTags">{{ $t('tagsView.closeAll') }}
      </div>
    </div>
    <div class="avue-tags__box">
      <el-tabs v-model="active"
               type="card"
               @contextmenu="handleContextmenu"
               :closable="tagLen!==1"
               @tab-click="openTag"
               @edit="menuTag">
        <el-tab-pane v-for="(item,index) in tagList"
                     :key="index"
                     :label="generateTitle(item)"
                     :name="item.fullPath">
          <template #label>
            <span>
              {{ generateTitle(item) }}
              <i class="el-icon-refresh"
                 :class="{'turn':refresh}"
                 @click="handleRefresh"
                 v-if="active===item.fullPath"></i>
            </span>
          </template>

        </el-tab-pane>

      </el-tabs>
      <el-dropdown class="avue-tags__menu">
        <el-button type="primary">
          {{ $t('tagsView.menu') }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="menuTag(tag.fullPath, 'remove')">{{
                $t('tagsView.closeCurrent')
              }}
            </el-dropdown-item>
            <el-dropdown-item @click="closeOthersTags">{{ $t('tagsView.closeOthers') }}</el-dropdown-item>
            <el-dropdown-item @click="closeAllTags">{{ $t('tagsView.closeAll') }}</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>

  </div>
</template>
<script>
import useCommonStore from "@/stores/useCommonStore";
import useRouterStore from "@/stores/useRouterStore";
import website from "@/config/website";


export default {
  name: "tags",
  data() {
    return {
      refresh: false,
      active: "",
      contentmenuX: "",
      contentmenuY: "",
      contextmenuFlag: false,
      contextMenuTagName: null,
    };
  },
  watch: {
    tag: {
      handler(val) {
        this.active = val.fullPath;
      },
      immediate: true,
    },
    contextmenuFlag() {
      window.addEventListener("mousedown", this.watchContextmenu);
    }
  },
  computed: {
    routerStore() {
      return useRouterStore()
    },
    commonStore() {
      return useCommonStore();
    },
    tagWel() {
      return this.routerStore.tagWel
    },
    tagList() {
      return this.routerStore.tagList
    }, tag() {
      return this.routerStore.tag
    }, setting() {
      return this.commonStore.setting
    },
    labelKey() {
      return website.menuConfig.label
    },

    tagLen() {
      return this.tagList.length || 0;
    }
  },
  created() {
  },
  methods: {
    openSearch() {
      this.commonStore.setIsSearch(true)
    },
    handleRefresh() {
      this.refresh = true;
      this.commonStore.setIsRefresh(false)
      setTimeout(() => {
        this.commonStore.setIsRefresh(true)
      }, 100)
      setTimeout(() => {
        this.refresh = false;
      }, 500)
    },
    generateTitle(item) {
      const props = {};
      let metaProps = item.meta || {}
      metaProps = metaProps.props || {}
      props.labelKey = metaProps.labelKey || website.menuConfig.label || "label"
      let menuItem = {...item};
      return this.$router.$avueRouter.generateTitle(menuItem, props);
    },
    watchContextmenu(event) {
      if (!this.$el.contains(event.target) || event.button !== 0) {
        this.contextmenuFlag = false;
      }
      window.removeEventListener("mousedown", this.watchContextmenu);
    },
    handleContextmenu(event) {
      let target = event.target;
      let flag = false;
      this.contextMenuTagName = null;
      // console.log("鼠标右键", target)
      if (target.className.indexOf("el-tabs__item") > -1) {
        flag = true
      } else if (target.parentNode.className.indexOf("el-tabs__item") > -1) {
        target = target.parentNode;
        flag = true;
      }

      if (flag) {
        event.preventDefault();
        event.stopPropagation();
        this.contentmenuX = event.clientX;
        this.contentmenuY = event.clientY;
        this.contextMenuTagName = target.getAttribute("aria-controls").slice(5);
        this.contextmenuFlag = true;
      }
    },
    menuTag(value, action) {
      if (action === "remove") {
        let {tag, key} = this.findTag(value);
        this.routerStore.delTag(tag);
        if (tag.fullPath === this.tag.fullPath) {
          tag = this.tagList[key - 1]
          this.$router.push({path: tag.path, query: tag.query})
        }
      }
    },
    openTag(item) {
      let value = item.props.name
      let {tag} = this.findTag(value)
      this.$router.push({
        path: tag.path,
        query: tag.query
      })
    },
    findTag(fullPath) {
      let tag = this.tagList.find(item => item.fullPath === fullPath);
      let key = this.tagList.findIndex(item => item.fullPath === fullPath);
      return {tag, key}
    },
    closeTag() {
      this.contextmenuFlag = false;
      this.menuTag(this.contextMenuTagName, "remove")
      this.contextMenuTagName = null;

    },
    closeOthersTags() {
      this.contextmenuFlag = false;
      this.routerStore.delOtherTag();

    },
    closeAllTags() {
      this.contextmenuFlag = false;
      this.routerStore.delAllTag();

      this.$router.push(this.tagWel);
    }
  }
};
</script>


